package com.hjm.mall.api;

import com.hjm.mall.api.param.AddressParam;
import com.hjm.mall.config.annotation.TokenToMallUserId;
import com.hjm.mall.service.impl.AddressServiceImpl;
import com.hjm.mall.util.Result;
import com.hjm.mall.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@ResponseBody
@RequestMapping("/api")
@Api(value = "order",tags = "哈基米商城——地址接口")
public class AddressController {
    @Autowired
    AddressServiceImpl addressService;

    @GetMapping("/address")
    @ApiOperation(value = "根据token获取地址", notes = "需要token验证")
    public Result getAddressByUserId(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId){
        return ResultGenerator.genSuccessResult(addressService.getAddressListByUserId(userId));
    }
    @GetMapping("/address/def")
    @ApiOperation(value = "根据token获取默认地址", notes = "需要token验证")
    public Result getAddressByUserIdDefault(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId){
        return ResultGenerator.genSuccessResult(addressService.getAddressListByUserIdDefault(userId));
    }

    @PostMapping("/address/insert")
    @ApiOperation(value = "新增地址", notes = "需要token验证")
    public Result insetAddress(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId,
            @RequestBody
            @ApiParam("地址信息")
                    AddressParam addressParam){
        boolean res = addressService.inset(userId, addressParam);
        if(res)
            return ResultGenerator.genSuccessResult("新增成功");
        return ResultGenerator.genFailResult("新增失败");
    }

    @DeleteMapping("/address")
    @ApiOperation(value = "删除地址", notes = "")
    public Result deleteOrder(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId,
            @ApiParam("地址id")
            @RequestParam("id")
                    Integer id){
        boolean res = addressService.delete(id);
        if(res)
            return ResultGenerator.genSuccessResult("删除成功");
        return ResultGenerator.genFailResult("删除失败");
    }
    @PutMapping("/address")
    @ApiOperation(value="修改订单信息", notes="")
    public Result updateOrder(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId,
            @ApiParam("地址id")
            @RequestParam("id")
                    Integer id,
            @RequestBody
            @ApiParam("地址信息")
            AddressParam addressParam){
        boolean res = addressService.update(userId,id,addressParam);
        if(res)
            return ResultGenerator.genSuccessResult("修改成功");
        return ResultGenerator.genFailResult("修改失败");
    }
}
